package org.lwz.space.controller;

import org.lwz.space.model.Article;
import org.lwz.space.model.ArticleType;
import org.lwz.space.model.User;
import org.lwz.space.service.ArticleService;
import org.lwz.space.service.ArticleTypeService;
import org.lwz.space.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Liaowz on 2015/11/21.
 */
@Controller
@RequestMapping("/article")
public class ArticleController {
    private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);


    @Resource
    private UserService userService;

    @Resource
    private ArticleService articleService;

    @Resource
    private ArticleTypeService articleTypeService;

    @RequestMapping(value = "/articles", method = RequestMethod.GET)
    public String articles(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {


        List<ArticleType> articleTypeList = this.articleTypeService.list();
        model.addAttribute("articleTypes", articleTypeList);

        List<Article> articleList;
        String from = request.getParameter("from");
        if(from != null){
            articleList = articleService.list();
        }else {
            Map<String,String> map = request.getParameterMap();
//            articleList = articleService.list(map);
        }


//        model.addAttribute("articles", articleList);

        return "/article/articles";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {

        String id = request.getParameter("id");
        articleService.delete(Integer.valueOf(id).intValue());

        logger.info("cccccccccccccccccccccc");

        return "redirect:articles";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {

        List<ArticleType> articleTypeList = this.articleTypeService.list();
        model.addAttribute("articleTypes", articleTypeList);

        return "/article/add";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {

        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String articleType = request.getParameter("articleType");

        ArticleType type = articleTypeService.getArticleType(articleType);

        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setArticleType(type);

        String username = (String) session.getAttribute("username");
        String password = (String) session.getAttribute("password");
        User user = userService.getUser(username, password);

        article.setUser(user);
        article.setCreateDate(new Date());
        article.setModifiedDate(new Date());


        articleService.save(article);

        return "redirect:articles";
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {

        String id = request.getParameter("id");
        Article article = articleService.getArticle(Integer.valueOf(id));

        model.addAttribute("article", article);

        return "/article/detail";
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public String modify(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {

        String id = request.getParameter("id");
        Article article = articleService.getArticle(Integer.valueOf(id));

        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String articleType = request.getParameter("articleType");

        ArticleType type = articleTypeService.getArticleType(articleType);

        article.setTitle(title);
        article.setContent(content);
        article.setArticleType(type);
        article.setModifiedDate(new Date());

        articleService.update(article);

        return "redirect:articles";
    }
}
